import re
import pymysql


def stringToInt(string: str):
    string = string.replace(',', '')

    patten = "[-+]?[0-9]*\.?[0-9]+"
    a = float(re.search(patten, string).group())
    if '万' in string:
        a = a * 10000
    return int(a)


class Book():
    # 标题
    title = ''
    # 作者名
    author = ''
    # 封面
    cover = ''
    # 总点击
    clickTotal = 0
    # 月点击
    clickMonth = 0
    # 周点击
    clickWeek = 0
    # 总推荐
    recommendTotal = 0
    # 月推荐
    recommendMonth = 0
    # 周推荐
    recommendWeek = 0
    # 总月票
    monthTicketTotal = 0
    # 总字数
    wordTotal = 0
    # 收藏
    collect = 0

    def parseResponse(self, response):
        self.title = response.selector.xpath('//h3[@class="title"]/text()').get()
        self.author = response.selector.xpath('//h3[@class="title"]/span/a/text()').get()
        self.cover = response.selector.xpath('//div[@class="cover ly-fl"]/img/@src').get()

        clickTotal_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[2]/i/text()').get()
        clickMonth_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[3]/i/text()').get()
        clickWeek_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[4]/i/text()').get()

        # 原始数据
        self.clickTotal = stringToInt(clickTotal_origin)
        self.clickMonth = stringToInt(clickMonth_origin)
        self.clickWeek = stringToInt(clickWeek_origin)

        recommendTotal_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[6]/i/text()').get()
        recommendMonth_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[7]/i/text()').get()
        recommendWeek_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[8]/i/text()').get()

        # 原始数据
        self.recommendTotal = stringToInt(recommendTotal_origin)
        self.recommendMonth = stringToInt(recommendMonth_origin)
        self.recommendWeek = stringToInt(recommendWeek_origin)

        # 原始数据
        wordTotal_origin = response.selector.xpath('//div[@class="book-property clearfix"]/span[9]/i/text()').get()
        monthTicketTotal_origin = response.selector.xpath('//li[@class="month"]/h3/text()').get()
        collect_origin = response.selector.xpath('//p[@class="book-grade"]/b[2]/text()').get()

        self.wordTotal = stringToInt(wordTotal_origin)
        self.monthTicketTotal = stringToInt(monthTicketTotal_origin)
        self.collect = stringToInt(collect_origin)

    def printProperties(self):
        print(self.title)
        print(self.author)
        print(self.cover)
        print(self.clickTotal)
        print(self.clickMonth)
        print(self.clickWeek)
        print(self.recommendTotal)
        print(self.recommendMonth)
        print(self.recommendWeek)
        print(self.wordTotal)
        print(self.monthTicketTotal)
        print(self.collect)

    def insertBook(self):
        db = pymysql.connect(host='localhost', port=3307, user='root', passwd='123456', db='ciweimao', charset='utf8')

        cursor = db.cursor()

        sql = "INSERT INTO ciweimao.book(title, author, cover," \
              "click_total, click_month, click_week, " \
              "recommend_total, recommend_month, recommend_week, " \
              "word_total, monthticket_total, collect)" \
              " values('%s', '%s', '%s', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i')" % \
              (self.title, self.author, self.cover,
               self.clickTotal, self.clickMonth, self.clickWeek,
               self.recommendTotal, self.recommendMonth, self.recommendWeek,
               self.wordTotal, self.monthTicketTotal, self.collect)

        try:
            # 执行sql语句
            cursor.execute(sql)
            db.commit()
        except:
            print('exception')

        db.close()
